﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using KorbeRTR.Models;

namespace KorbeRTR
{
    public partial class FormUserPurview : Form
    {
        public FormUserPurview()
        {
            InitializeComponent();
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            var gl = Global.GetInstance();
            if(gl.CurrentUser.Check用户权限设定权限())
            {
                SetEnable();
            }
        }

        /// <summary>
        /// 设置输入用户名密码等使能
        /// </summary>
        private void SetEnable()
        {

            tsBtnAdd.Enabled = false;
            tsBtnChange.Enabled = false;
            tsBtnDelete.Enabled = false;

            tsBtnSave.Enabled = true;
            tsBtnRevoke.Enabled = true;
            txt用户名.Enabled = true;
            txt密码.Enabled = true;
            grid用户权限.Enabled = true;


        }

        private void SetDisable()
        {
            tsBtnAdd.Enabled = true;
            tsBtnChange.Enabled = true;
            tsBtnDelete.Enabled = true;

            tsBtnSave.Enabled = false;
            tsBtnRevoke.Enabled = false;
            txt用户名.Enabled = false;
            txt密码.Enabled = false;
            grid用户权限.Enabled = false;
        }

        private void FormUserPurview_Load(object sender, EventArgs e)
        {
            SetDisable();
            //更新treeview
            UpdateTree(treeView1);       
            //init用户权限表
            InitGrid(grid用户权限);

        }

        private void UpdateTree(TreeView tree)
        {
            tree.Nodes.Clear();
            var list = User.ListAll();

            TreeNode root = new TreeNode();
            root.Name = "root";
            root.Text = "用户";
            tree.Nodes.Add(root);

        
            foreach (var iter in list)
            {
                TreeNode node = new TreeNode();
                node.Name = iter.Id.ToString();
                node.Text = iter.UserName;
                root.Nodes.Add(node);
            }
            tree.ExpandAll();
        }

        private void InitGrid(FlexCell.Grid grid)
        {
            grid.AutoRedraw = false;
          
            grid.DisplayRowNumber = true;
            //  AlarmRecordView.ListAll();
            // 设置行数/列数

            grid.Rows = 14;
            grid.Cols = 3;

            grid.Column(0).Width = 30;
            grid.Column(1).Width = 300;
   

            grid.Column(2).CellType = FlexCell.CellTypeEnum.CheckBox;
        

            grid.Cell(0, 0).Text = "序号";
            grid.Cell(0, 1).Text = "标签";
            grid.Cell(0, 2).Text = "权限";

            string[] labels = new string[] { "产品基本数据(新增/修改/删除)",
                                             "设定料号(投料/变更/删除)",
                                             "温控器温度设定",
                                             "AH药水自动添加设定",
                                             "用户权限设定",
                                             "系统数据",
                                             "退出DMS系统",
                                             "更改密码",
                                             "故障代码设定",
                                             "泵浦自动开关设定",
                                             "累计值清零",
                                             "洗配槽设定",
                                             "浮板位置初始设定",
            };
            for(int i=1;i<grid.Rows;i++)
            {
                grid.Cell(i, 1).Text = labels[i - 1];
            }
                 
            grid.AutoRedraw = true;
            grid.Refresh();
        }

       

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if(e.Button != System.Windows.Forms.MouseButtons.Left)
            {
                return;
            }
            if (e.Node.Level != 1)//1级结点
            {
                return;
            }

            var id = Convert.ToInt64(e.Node.Name);
            var list = User.ListAll();
            var user = list.Find(u => u.Id == id);
            if (user != null)
            {
                txt用户名.Text = user.UserName;
                txt密码.Text = user.Password;
                int row = 1;
                grid用户权限.Cell(row++, 2).Text = user.产品数据操作权限.ToString();
                grid用户权限.Cell(row++, 2).Text = user.生产料号操作权限.ToString();
                grid用户权限.Cell(row++, 2).Text = user.温控器温度设定权限.ToString();
                grid用户权限.Cell(row++, 2).Text = user.自动添加设定权限.ToString();
                grid用户权限.Cell(row++, 2).Text = user.用户权限设定权限.ToString();
                grid用户权限.Cell(row++, 2).Text = user.系统数据权限.ToString();
                grid用户权限.Cell(row++, 2).Text = user.软件退出权限.ToString();
                grid用户权限.Cell(row++, 2).Text = user.更改密码权限.ToString();
                grid用户权限.Cell(row++, 2).Text = user.故障代码设定权限.ToString();
                grid用户权限.Cell(row++, 2).Text = user.泵浦自动开关设定权限.ToString();
                grid用户权限.Cell(row++, 2).Text = user.累计值清零权限.ToString();
                grid用户权限.Cell(row++, 2).Text = user.洗配槽参数设定权限.ToString();
                grid用户权限.Cell(row++, 2).Text = user.浮板位置设定权限.ToString();
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsBtnSave_Click(object sender, EventArgs e)
        {
            //修改
            User user = null;
            if (changeFlag)
            {
                var id = Convert.ToInt64(treeView1.SelectedNode.Name);

                var list = User.ListAll();
                user = list.Find(u => u.Id == id);
                if (user == null)
                {
                    return;
                }
             
            }
            else
            {
                user = new User();
            }

            user.UserName = txt用户名.Text.Trim();
            user.Password = txt密码.Text.Trim();

            int row = 1;
            user.产品数据操作权限 = grid用户权限.Cell(row++, 2).Text.ToBool();
            user.生产料号操作权限 = grid用户权限.Cell(row++, 2).Text.ToBool();
            user.温控器温度设定权限 = grid用户权限.Cell(row++, 2).Text.ToBool();
            user.自动添加设定权限 = grid用户权限.Cell(row++, 2).Text.ToBool();
            user.用户权限设定权限 = grid用户权限.Cell(row++, 2).Text.ToBool();
            user.系统数据权限 = grid用户权限.Cell(row++, 2).Text.ToBool();
            user.软件退出权限 = grid用户权限.Cell(row++, 2).Text.ToBool();
            user.更改密码权限 = grid用户权限.Cell(row++, 2).Text.ToBool();
            user.故障代码设定权限 = grid用户权限.Cell(row++, 2).Text.ToBool();
            user.泵浦自动开关设定权限 = grid用户权限.Cell(row++, 2).Text.ToBool();
            user.累计值清零权限 = grid用户权限.Cell(row++, 2).Text.ToBool();
            user.洗配槽参数设定权限 = grid用户权限.Cell(row++, 2).Text.ToBool();
            user.浮板位置设定权限 = grid用户权限.Cell(row++, 2).Text.ToBool();

            if (changeFlag)
            {
                changeFlag = false;
                User.Update(user);

                //添加记录
                var record = new OperationRecord()
                {
                    Time = DateTime.Now,
                    User = Global.GetInstance().CurrentUser.UserName,
                    Operation = "修改用户",
                    OperationType = "用户权限"
                };
                OperationRecord.Add(record);
            }
            else
            {
                if(user.UserName == "admin")
                {
                    MessageBox.Show("不能新增用户名为admin的用户。");
                }
                else
                {
                    User.Add(user);

                    //添加记录  这个地方没有记录成功
                    var record = new OperationRecord()
                    {
                        Time = DateTime.Now,
                        User = Global.GetInstance().CurrentUser.UserName,
                        Operation = "新增用户",
                        OperationType = "用户权限"
                    };
                    OperationRecord.Add(record);

                }
                
            }
      
            UpdateTree(treeView1);
            SetDisable();
        }

        //修改标识
        public bool changeFlag = false;

        //修改
        private void tsBtnChange_Click(object sender, EventArgs e)
        {
            var node = treeView1.SelectedNode;
            if (node != null && node.Level == 1)
            {
                SetEnable();
                changeFlag = true;
                txt用户名.Enabled = false;
               
            }
            
        }

        private void tsBtnRevoke_Click(object sender, EventArgs e)
        {
            SetDisable();
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {

            if (treeView1.SelectedNode != null && treeView1.SelectedNode.Level == 1)
            {
                if(treeView1.SelectedNode.Text !="admin")
                {
                    
                    var user = new User();
                    user.Id = Convert.ToInt64(treeView1.SelectedNode.Name);
                    User.Delete(user);
                    UpdateTree(treeView1);
                    SetDisable();
                }
                else
                {
                    MessageBox.Show("admin用户不能删除。");
                }
                    

            }
        }
    }

    public static class Extersion
    {
        public static bool ToBool(this string s)
        {
            if (string.IsNullOrWhiteSpace(s)) return false;

            switch(s.ToUpper())
            {
                case "0":
                case "FALSE":
                    return false;

                case "1":
                case "TRUE":
                    return true;

                default:
                    return false;
            }
        }
    }
}
